import com.alibaba.fastjson.JSON;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/*
 * @author changfei
 * @date  2023/11/27 20:25
 * @description
 */
public class TestPoi {
 public static void main(String[] args) throws IOException {
  String file="/Users/changfei/Documents/预算批量新增_2023112708.xlsx";

  // 1.创建一个工作薄
  FileInputStream fileInputStream = new FileInputStream(file);

  XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fileInputStream);
  XSSFSheet sheet = xssfWorkbook.getSheetAt(0);

  List<SaveBudgetBillExcelReq> budgetBillReqList = new ArrayList<>();
  // 得到最后一行的行数
  int rowNum = sheet.getLastRowNum();
  String sheetName = sheet.getSheetName();
  System.out.println(sheetName);
  System.out.println(rowNum);
  for (int i = 2; i <= rowNum; i++) {
   Row row = sheet.getRow(i);
   boolean rowEmpty = isRowEmpty(row);
   if(rowEmpty)continue;
   SaveBudgetBillExcelReq saveBudgetBillExcelReq=new SaveBudgetBillExcelReq();
   //预算单据编号
   saveBudgetBillExcelReq.setBudgetBillCfgCode(row.getCell(0).toString());
   saveBudgetBillExcelReq.setBudgetBillCfgTitle(row.getCell(1).toString());
   saveBudgetBillExcelReq.setBudgetVersionCode(row.getCell(2).toString());
   saveBudgetBillExcelReq.setBudgetVersionName(row.getCell(3).toString());
   saveBudgetBillExcelReq.setBudgetPrincipalCode(row.getCell(4).toString());
   saveBudgetBillExcelReq.setBudgetPrincipalName(row.getCell(5).toString());
   saveBudgetBillExcelReq.setBudgetCenterCode(row.getCell(6).toString());
   saveBudgetBillExcelReq.setBudgetCenterName(row.getCell(7).toString());
   saveBudgetBillExcelReq.setBudgetSubjectCode(row.getCell(8).toString());
   saveBudgetBillExcelReq.setBudgetSubjectName(row.getCell(9).toString());
   saveBudgetBillExcelReq.setBudgetPeriod(row.getCell(10).toString());
   saveBudgetBillExcelReq.setBudgetAmount(row.getCell(11).toString());
   saveBudgetBillExcelReq.setRemark(row.getCell(12).toString());
   System.out.println(saveBudgetBillExcelReq);
   System.out.println(JSON.toJSONString(saveBudgetBillExcelReq));
   budgetBillReqList.add(saveBudgetBillExcelReq);
   System.out.println(msg());
  }


 }

 private static boolean isRowEmpty(Row row){
  if(row==null){
   return true;
  }
  boolean isEmpty=true;
  for (int cellIndex=0;cellIndex<row.getLastCellNum();cellIndex++){
   Cell cell = row.getCell(cellIndex);
   if(cell!=null && cell.getCellType()!= CellType.BLANK){
    isEmpty=false;
    break;
   }
  }
  return isEmpty;
 }


 public static String msg(){
  String s="成功";
  try{
   if(1==12)
   return "111";
  }catch (Exception e){
   s= "2222";
  }
  return s;
 }

}
